package b1.scenes.detect
{
	import b1.conifg.*;
	import b1.controls.tab.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.SceneContainer;
	import b1.scenes.crime.*;
	import b1.scenes.home.*;
	import b1.scenes.home.wanted.*;
	import b1.scenes.home.police.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.text.*;
	import flash.media.Sound;

	public class DetectDiaglog extends TabController
	{
		private static var instance:DetectDiaglog;
		private var parentScene:Sprite;
		
		public function DetectDiaglog()
		{
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("TabControllerBackground");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("TabTitleBackground");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton1");
			super(TabControllerBackground, CloseButton);
			var tab:Tab = new Tab("案件", 4, 2, 5, 3);
			this.addTab(TabTitleBackground, tab);
//			var tab:Tab = new Tab("我自己", 4, 2, 5, 3);
//			this.addTab(TabTitleBackground, tab);
//			tab = new Tab("邻居", 4, 2, 5, 3);
//			this.addTab(TabTitleBackground, tab);
//			tab = new Tab("好友", 4, 2, 5, 3);
//			this.addTab(TabTitleBackground, tab);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	titleText.width = 45;
        	titleText.wordWrap = true;
        	titleText.filters = [BFont.greenFontGlowFilter];
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 20, 0x000000, true, true);
			titleText.text = "请 选 择 探 案 的 对 象 ？ ";
			titleText.x = 80;
			titleText.y = 60;
			addChild(titleText);
		}

		public static function getInstance():DetectDiaglog
		{
			if (!instance)
				instance = new DetectDiaglog();
			return instance;
		}
		
		public function open(parentScene:Sprite):void
		{
			if (!parentScene.contains(this))
			{
				this.parentScene = parentScene;
				var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
				var detectiveAnimalId:int = parseInt(player.ownAnimals[0]);
				ServerHelper.getInstance().getAllCases(detectiveAnimalId, onOpenComplete);
			}
			
		}

		private function onOpenComplete():void
		{
			var battleCases:Object = PlayerInfoHelper.getInstance().battleCases;
			var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			//去掉原来案件
			for (var i:int; i < this.tabs.length; i++)
			{
				this.tabs[i].removeAll();
			}
			//特殊案件
			for (j = 0; j < Config.specialCaseConfigs.length; j++)
			{
				var specialCaseConfig:SpecialCaseConfig = Config.specialCaseConfigs[j];
				if (me.accomplishedTaskId == specialCaseConfig.taskId - 1)
				{
					var specialCaseItem:SpecialCaseItem = new SpecialCaseItem(specialCaseConfig);
					this.tabs[0].addItem(specialCaseItem, 125, 125);
				}
			}
			//节日特殊案件
			for (var j:int = 0; j < Config.festivalSpecialCaseConfigs.length; j++)
			{
				var festivalSpecialCaseConfig:SpecialCaseConfig = Config.festivalSpecialCaseConfigs[j];
				if (me.accomplishedFestivalTaskId == festivalSpecialCaseConfig.taskId - 1)
				{
					var festivalSpecialCaseItem:SpecialCaseItem = new SpecialCaseItem(festivalSpecialCaseConfig);
					this.tabs[0].addItem(festivalSpecialCaseItem, 125, 125);
				}
			}
			//其他案件
			for (var caseId:String in battleCases)
			{
				var battleCase:BattleCaseInfo = battleCases[caseId];
				var ownerId:int = PlayerInfoHelper.getInstance().animals[battleCase.loserAnimalId].ownerId;
				var owner:PlayerInfo = PlayerInfoHelper.getInstance().players[ownerId];
				var detectItem:DetectItem = new DetectItem(battleCase);
//				this.tabs[owner.type].addItem(detectItem, 125, 125);
				this.tabs[0].addItem(detectItem, 125, 125);
			}

			this.x = (BFont.stageWidth - this.width) / 2;
			this.y = 40;
			parentScene.addChild(this);

			WantedTab.getInstance().close();
			Store.getInstance().close();
			CrimeDialog.getInstance().close();
			PoliceStation.getInstance().close();
			BottomNavigator.getInstance().hide();
			//播放音效
			var SoundButton1:Class = ResourceHelper.getInstance().getClassByName("SoundButton1");
			var sound:Sound = new SoundButton1();
			sound.play(); 
			//添加新手导引
			if (SceneContainer.getInstance().step == 9)
				SceneContainer.getInstance().addStep();
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
	}
}